/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.edu.fatec.ads.classes;

import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Transient;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

/**
 *
 * @author Roberta Ferreira
 */
@Entity
public class Cartao extends AbstractTO{

    private int codigo;
    private List<Local> locais;
    private List<Emprestimo> emprestimos;

    public Cartao() {
    }

    public Cartao(int codigo) {
        this.codigo = codigo;
    }


    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="cd_cartao", length=4)
    public int getCodigo() {
        return codigo;
    }

    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }

    /**
     * @return the locais
     */
    @ManyToMany(fetch=FetchType.LAZY)
    @JoinTable(name="LOCAL_CARTAO",
               joinColumns=@JoinColumn(name="cd_cartao", nullable=false),
               inverseJoinColumns={@JoinColumn(name="cd_sequencial", referencedColumnName="cd_sequencial", nullable=false),
                                   @JoinColumn(name="tp_local", referencedColumnName="tp_local", nullable=false)})
    public List<Local> getLocais() {
        return locais;
    }

    /**
     * @param locais the locais to set
     */
    public void setLocais(List<Local> locais) {
        this.locais = locais;
    }

    /**
     * @return the emprestimos
     */
    @OneToMany(cascade=CascadeType.ALL, mappedBy="cartao")
    @Fetch(FetchMode.SELECT)
    public List<Emprestimo> getEmprestimos() {
        return emprestimos;
    }

    /**
     * @param emprestimos the emprestimos to set
     */
    public void setEmprestimos(List<Emprestimo> emprestimos) {
        this.emprestimos = emprestimos;
    }

    @Transient
    public Serializable getPK() {
        return this.getCodigo();
    }

    @Override
    public void setPK(Serializable identifier) {
        this.setCodigo((Integer) identifier);
    }
    
    @Override
    public String toString() {
        return "Cartao[codigo=" + this.getCodigo() + "]";
    }

}